package com.example.plugin.ui;

import com.example.plugin.config.GenerateConfig;
import com.example.plugin.config.PanelModel;
import com.example.plugin.setting.GenerateSetting;
import com.intellij.database.psi.DbTable;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.psi.PsiElement;
import com.intellij.ui.components.JBCheckBox;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;

/**
 * Generate面板
 *
 * @author lipan
 * @date 2022/1/16 9:36
 * @email 13160080997@163.com
 **/
public class GenerateUI extends JDialog {

    private Project project;

    private AnActionEvent anActionEvent;

    private JTextField tableTextField = new JTextField(12);
    private JTextField entityTextField = new JTextField(12);
    private TextFieldWithBrowseButton entityFolderBtn = new TextFieldWithBrowseButton();
    private TextFieldWithBrowseButton daoFolderBtn = new TextFieldWithBrowseButton();
    private TextFieldWithBrowseButton serviceFolderBtn = new TextFieldWithBrowseButton();
    private TextFieldWithBrowseButton controllerFolderBtn = new TextFieldWithBrowseButton();
    private TextFieldWithBrowseButton xmlFolderBtn = new TextFieldWithBrowseButton();
    //复选框，选中时生成对应文件
    private JBCheckBox entityCheck = new JBCheckBox();
    private JBCheckBox daoCheck = new JBCheckBox();
    private JBCheckBox serviceCheck = new JBCheckBox();
    private JBCheckBox controllerCheck = new JBCheckBox();
    private JBCheckBox xmlCheck = new JBCheckBox();

    public GenerateUI(AnActionEvent anActionEvent){
        this.project = anActionEvent.getProject();
        this.anActionEvent = anActionEvent;
        //默认全选中
        entityCheck.setSelected(true);
        daoCheck.setSelected(true);
        serviceCheck.setSelected(true);
        controllerCheck.setSelected(true);
        xmlCheck.setSelected(true);

        setModal(true);
        setTitle("generate");
        setSize(800,500);
        setLocationRelativeTo(null);
        //面板
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());
        //左侧面板
        JPanel leftPanel = new JPanel();
        leftPanel.setPreferredSize(new Dimension(170,500));
        leftPanel.setBorder(new TitledBorder(new LineBorder(new Color(51,163,220)),"history config"));
        HistoryPanel historyPanel = new HistoryPanel(this, GenerateSetting.settings.getPanelModelMap().keySet());
        leftPanel.add(historyPanel);
        //中部面板
        JPanel centerPanel = new JPanel();
        centerPanel.setBorder(new TitledBorder(new LineBorder(new Color(51,163,220)),"class config"));
        centerPanel.setPreferredSize(new Dimension(600,500));
        //中部-顶部
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        topPanel.setPreferredSize(new Dimension(600,50));
        JPanel topGridPanel = new JPanel(new GridLayout(1,2,10,10));

        JPanel tablePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel tableLabel = new JLabel("表名：");
        tableLabel.setHorizontalAlignment(SwingConstants.LEFT);
        tablePanel.add(tableLabel);
        PsiElement[] psiElements = this.anActionEvent.getData(LangDataKeys.PSI_ELEMENT_ARRAY);
        PsiElement psiElement = psiElements[0];
        DbTable db = (DbTable)psiElement;
        tableTextField.setText(db.getName());
        tablePanel.add(tableTextField);

        JPanel entityNamePanel = new JPanel();
        JLabel entityLabel = new JLabel("Entity名：");
        entityNamePanel.add(entityLabel);
        entityNamePanel.add(entityTextField);
        topGridPanel.add(tablePanel);
        topGridPanel.add(entityNamePanel);

        topPanel.add(topGridPanel);
        centerPanel.add(topPanel);
        //中部-第二部分
        JPanel topPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        topPanel2.setPreferredSize(new Dimension(600,320));
        JPanel gridPanel2 = new JPanel(new GridLayout(5,1,10,5));
        //entity面板
        JPanel entityPanel = new JPanel();
        JPanel entityFolderPanel = new JPanel();
        JLabel entityFolderLabel = new JLabel("entity folder：");
        entityFolderLabel.setPreferredSize(new Dimension(120,20));
        entityFolderLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        entityFolderBtn.setTextFieldPreferredWidth(45);
        entityFolderBtn.setText(project.getBasePath());
        entityFolderBtn.addBrowseFolderListener(new TextBrowseFolderListener(FileChooserDescriptorFactory.createSingleFileOrFolderDescriptor()) {
            @Override
            public void actionPerformed(ActionEvent e) {
                super.actionPerformed(e);
            }
        });
        entityFolderPanel.add(entityFolderLabel);
        entityFolderPanel.add(entityFolderBtn);
        entityFolderPanel.add(entityCheck);
        entityPanel.add(entityFolderPanel);
        //DAO面板
        JPanel daoPanel = new JPanel();
        JPanel daoFolderPanel = new JPanel();
        JLabel daoFolderLabel = new JLabel("dao folder：");
        daoFolderLabel.setPreferredSize(new Dimension(120,20));
        daoFolderLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        daoFolderBtn.setTextFieldPreferredWidth(45);
        daoFolderBtn.setText(project.getBasePath());
        daoFolderBtn.addBrowseFolderListener(new TextBrowseFolderListener(FileChooserDescriptorFactory.createSingleFileOrFolderDescriptor()) {
            @Override
            public void actionPerformed(ActionEvent e) {
                super.actionPerformed(e);
            }
        });
        daoFolderPanel.add(daoFolderLabel);
        daoFolderPanel.add(daoFolderBtn);
        daoFolderPanel.add(daoCheck);
        daoPanel.add(daoFolderPanel);
        //Service面板
        JPanel servicePanel = new JPanel();
        JPanel serviceFolderPanel = new JPanel();
        JLabel serviceFolderLabel = new JLabel("service folder：");
        serviceFolderLabel.setPreferredSize(new Dimension(120,20));
        serviceFolderLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        serviceFolderBtn.setTextFieldPreferredWidth(45);
        serviceFolderBtn.setText(project.getBasePath());
        serviceFolderBtn.addBrowseFolderListener(new TextBrowseFolderListener(FileChooserDescriptorFactory.createSingleFileOrFolderDescriptor()) {
            @Override
            public void actionPerformed(ActionEvent e) {
                super.actionPerformed(e);
            }
        });
        serviceFolderPanel.add(serviceFolderLabel);
        serviceFolderPanel.add(serviceFolderBtn);
        serviceFolderPanel.add(serviceCheck);
        servicePanel.add(serviceFolderPanel);

        //Controller面板
        JPanel controllerPanel = new JPanel();
        JPanel controllerFolderPanel = new JPanel();
        JLabel controllerFolderLabel = new JLabel("controller folder：");
        controllerFolderLabel.setPreferredSize(new Dimension(120,20));
        controllerFolderLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        controllerFolderBtn.setTextFieldPreferredWidth(45);
        controllerFolderBtn.setText(project.getBasePath());
        controllerFolderBtn.addBrowseFolderListener(new TextBrowseFolderListener(FileChooserDescriptorFactory.createSingleFileOrFolderDescriptor()) {
            @Override
            public void actionPerformed(ActionEvent e) {
                super.actionPerformed(e);
            }
        });
        controllerFolderPanel.add(controllerFolderLabel);
        controllerFolderPanel.add(controllerFolderBtn);
        controllerFolderPanel.add(controllerCheck);
        controllerPanel.add(controllerFolderPanel);

        //xml面板
        JPanel xmlPanel = new JPanel();
        JPanel xmlFolderPanel = new JPanel();
        JLabel xmlFolderLabel = new JLabel("xml folder：");
        xmlFolderLabel.setPreferredSize(new Dimension(120,20));
        xmlFolderLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        xmlFolderBtn.setTextFieldPreferredWidth(45);
        xmlFolderBtn.setText(project.getBasePath());
        xmlFolderBtn.addBrowseFolderListener(new TextBrowseFolderListener(FileChooserDescriptorFactory.createSingleFileOrFolderDescriptor()) {
            @Override
            public void actionPerformed(ActionEvent e) {
                super.actionPerformed(e);
            }
        });
        xmlFolderPanel.add(xmlFolderLabel);
        xmlFolderPanel.add(xmlFolderBtn);
        xmlFolderPanel.add(xmlCheck);
        xmlPanel.add(xmlFolderPanel);

        gridPanel2.add(entityPanel);
        gridPanel2.add(daoPanel);
        gridPanel2.add(servicePanel);
        gridPanel2.add(controllerPanel);
        gridPanel2.add(xmlPanel);
        topPanel2.add(gridPanel2);
        centerPanel.add(topPanel2);
        //下方按钮面板
        JPanel buttomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttomPanel.setPreferredSize(new Dimension(600,40));

        JPanel buttomButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton okButton = new JButton("OK");
        okButton.addActionListener(actionListener ->{
            this.onOk();
        });
        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(actionListener ->{
            dispose();
        });
        buttomButtonPanel.add(cancelButton);
        buttomButtonPanel.add(okButton);
        buttomPanel.add(buttomButtonPanel);

        centerPanel.add(buttomPanel);

        contentPanel.add(leftPanel,BorderLayout.WEST);
        contentPanel.add(centerPanel,BorderLayout.CENTER);
        setContentPane(contentPanel);
        setVisible(true);
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public AnActionEvent getAnActionEvent() {
        return anActionEvent;
    }

    public void setAnActionEvent(AnActionEvent anActionEvent) {
        this.anActionEvent = anActionEvent;
    }

    public JTextField getTableTextField() {
        return tableTextField;
    }

    public void setTableTextField(JTextField tableTextField) {
        this.tableTextField = tableTextField;
    }

    public JTextField getEntityTextField() {
        return entityTextField;
    }

    public void setEntityTextField(JTextField entityTextField) {
        this.entityTextField = entityTextField;
    }

    public TextFieldWithBrowseButton getEntityFolderBtn() {
        return entityFolderBtn;
    }

    public void setEntityFolderBtn(TextFieldWithBrowseButton entityFolderBtn) {
        this.entityFolderBtn = entityFolderBtn;
    }

    public TextFieldWithBrowseButton getDaoFolderBtn() {
        return daoFolderBtn;
    }

    public void setDaoFolderBtn(TextFieldWithBrowseButton daoFolderBtn) {
        this.daoFolderBtn = daoFolderBtn;
    }

    public TextFieldWithBrowseButton getServiceFolderBtn() {
        return serviceFolderBtn;
    }

    public void setServiceFolderBtn(TextFieldWithBrowseButton serviceFolderBtn) {
        this.serviceFolderBtn = serviceFolderBtn;
    }

    public TextFieldWithBrowseButton getControllerFolderBtn() {
        return controllerFolderBtn;
    }

    public void setControllerFolderBtn(TextFieldWithBrowseButton controllerFolderBtn) {
        this.controllerFolderBtn = controllerFolderBtn;
    }

    public TextFieldWithBrowseButton getXmlFolderBtn() {
        return xmlFolderBtn;
    }

    public void setXmlFolderBtn(TextFieldWithBrowseButton xmlFolderBtn) {
        this.xmlFolderBtn = xmlFolderBtn;
    }

    public JBCheckBox getEntityCheck() {
        return entityCheck;
    }

    public void setentityCheck(JBCheckBox entityCheck) {
        this.entityCheck = entityCheck;
    }

    public JBCheckBox getDaoCheck() {
        return daoCheck;
    }

    public void setDaoCheck(JBCheckBox daoCheck) {
        this.daoCheck = daoCheck;
    }

    public JBCheckBox getServiceCheck() {
        return serviceCheck;
    }

    public void setServiceCheck(JBCheckBox serviceCheck) {
        this.serviceCheck = serviceCheck;
    }

    public JBCheckBox getControllerCheck() {
        return controllerCheck;
    }

    public void setControllerCheck(JBCheckBox controllerCheck) {
        this.controllerCheck = controllerCheck;
    }

    public JBCheckBox getXmlCheck() {
        return xmlCheck;
    }

    public void setXmlCheck(JBCheckBox xmlCheck) {
        this.xmlCheck = xmlCheck;
    }

    private void onOk(){
        if(StringUtils.isBlank(this.getEntityTextField().getText())){
            Messages.showMessageDialog("请输入Entity名", "Notice", Messages.getInformationIcon());
            return;
        }
        PanelModel panelModel = new PanelModel();
        panelModel.setTableName(this.getTableTextField().getText().toLowerCase());
        String entityName = this.getEntityTextField().getText();
        //首字母大写
        entityName = entityName.substring(0,1).toUpperCase()+entityName.substring(1);
        panelModel.setEntityName(entityName);
        panelModel.setEntityPath(this.getEntityFolderBtn().getText());
        panelModel.setDaoPath(this.getDaoFolderBtn().getText());
        panelModel.setServicePath(this.getServiceFolderBtn().getText());
        panelModel.setControllerPath(this.getControllerFolderBtn().getText());
        panelModel.setXmlPath(this.getXmlFolderBtn().getText());
        panelModel.setEntityCheck(entityCheck.isSelected());
        panelModel.setDaoCheck(daoCheck.isSelected());
        panelModel.setServiceCheck(serviceCheck.isSelected());
        panelModel.setControllerCheck(controllerCheck.isSelected());
        panelModel.setXmlCheck(xmlCheck.isSelected());
        GenerateConfig generateConfig = new GenerateConfig();
        generateConfig.setAnActionEvent(this.getAnActionEvent());
        generateConfig.setPanelModel(panelModel);
        try {
            generateConfig.execute();
            GenerateSetting.settings.putPanelModel(this.getTableTextField().getText().toLowerCase(),panelModel);
            dispose();
            Messages.showInfoMessage("成功","SUCCESS");
        } catch (IOException e) {
            Messages.showErrorDialog(e.getMessage(),"ERROR");
        }
    }

}
